#pragma pack_matrix(row_major)

Texture2D baseTexture : register (t0);
Texture2D normalMap : register (t1);

SamplerState filters : register (s0);
struct vOutput {
	float4 wpos : POSITION;
	float3 normal : NORMAL;
	float2 uv : TEXCOORD;
	float3 tangent : TANGENT;
	float4 pos : SV_POSITION;
};

cbuffer OBJECT : register(b0){
	float4x4 mWorld;
}

cbuffer SCENE: register(b1){
	float4x4 mView;
	float4x4 mProj;
}

cbuffer LIGHT : register(b2){
	float4 color;
	float4 lightPos;
}

float4 main(vOutput input) : SV_TARGET
{

	float intensity;
	float3x3 TBN = float3x3(
		normalize(input.tangent),
		normalize(cross(input.tangent.xyz,input.normal.xyz)),
		normalize(input.normal)
		);
	TBN = transpose(TBN);
	float4 normal = normalMap.Sample(filters,input.uv);

	float3 lightDir = normalize(lightPos.xyz - input.wpos);

	float3 lightDirTan = normalize(mul(TBN,lightDir));
		float lightRatio = saturate(dot(lightDirTan,normal));
	float attenuation = 1.0f - saturate(length(lightPos-input.wpos)/10);
	intensity = lightRatio * attenuation * color;

		float specular;

	float3 reflected = reflect(normal,lightDirTan);
		specular = max(0.0,dot(reflected, input.tangent));
	specular = pow(specular,5);




	//return CalculateIntensity(input) * baseTexture.Sample(filters,input.uv);
	return intensity * color * (specular + baseTexture.Sample(filters,input.uv));

	//return float4(1, 0, 0, 1.0f);
}